<?php

//coder :
class con_Normal {

    private $result;

    function head($id, $title = '', $dis = '', $keyword = '', $top = true) {
        $homedef = new home();
        $resultD = $homedef->get_def_info();
        $home = new home();
        $this->result = $home->get_info_web($id);
        $this->result = array_merge($resultD[0], $this->result[0]);
        Controller::get_lib('func');
        $this->result['title_1'] = $this->result['title'];
        $this->result['title'] = !empty($title) ? $title : $this->result['title'];
        $this->result['dis'] = !empty($dis) ? $dis : $this->result['dis'];
        $this->result['key_word'] = !empty($keyword) ? $keyword : $this->result['key_word'];
        $home2 = new home();
        $this->result['menu'] = $home2->get_menu();
        Controller::get_view('header', $this->result);
        $top ? Controller::get_view('top', $this->result) : '';
    }

    function banner($type = false) {
        $dir = ROOT . 'public/banner/';
        $dh = opendir($dir);
        while (false !== ($filename = readdir($dh))) {
            $files[] = $filename;
        }
        $images['img'] = preg_grep('/\.jpg|.png$/i', $files);
        $images['type'] = $type;
        Controller::get_view('banner', $images);
    }

    function footer($type = false) {
        $end_home = new home();
        $this->result['type'] = $type;
        $this->result['menu'] = $end_home->get_menu();
        Controller::get_view('footer', $this->result);
    }

    function trang_chu() {
        $this->head(1,'Thiết kế web uy tín');
        $this->banner(true);
        Controller::get_lib('template');
        $result_news['url'] = URL . 'tin-tuc/';
        $hot_news = new home();
        $result_news['data_news'] = $hot_news->get_allNews(0, 5);
        Controller::get_view('trang_chu', $result_news);
        $this->footer(true);
    }

    function gioi_thieu() {
        $this->head(2);
        $this->banner();
        $result['url'] = URL . 'mau-website/';
        $this->footer();
    }

    function dat_hang() {
        $this->head(1, 'Đặt hàng');
        $this->banner();
        $data = array();
        $data['url'] = URL . 'dat-hang/';
        $bill = new home();
        $url = new Url(MAIN_SEG);
        $data['bill_info'] = $bill->get_bill_info($url->segment(2));
        $data['type'] = $url->segment(3);
        Controller::get_model('ad_table');
        Controller::get_lib('tbl_add');
        Controller::get_view('dat_hang', $data);
        $this->footer();
    }

    function mau_website() {
        $this->head(3);
        $this->banner();
        $url = new Url(MAIN_SEG);
        $seg = $url->segment(3);
        if (empty($seg)) {
            $seg2 = explode('.', $url->segment(2));
            $this->temp($url, empty($seg2[1]) ? 1 : $seg2[1]);
        } else {
            $this->view_itemplate($seg);
        }

        $this->footer();
    }

    function du_an_cong_ty() {
        $this->head(3);
        $this->banner();
        $this->temp(new Url(MAIN_SEG), 0);
        $this->footer();
    }

    function view_itemplate($id) {
        Controller::get_lib('template');
        Controller::get_model('ad_table');
        Controller::get_lib('tbl_add');
        $info_template = new home();
        $result_template['data_1'] = $info_template->get_temp_info($id);
        $comment = new home();
        $result_template['comment'] = $comment->get_comment($id);
        Controller::get_view('view_info', $result_template);
    }

    function demo() {
        $this->head(3, '', '', '', false);
        $url = new Url(MAIN_SEG);
        $seg = $url->segment(2);
        $info_template = new home();
        $this->result['data_1'] = $seg != '' ? $info_template->get_temp_info($seg) : array();
        Controller::get_view('demo', $this->result);
    }

    function tin_tuc() {
        $url = new Url(MAIN_SEG);
        $data = array();
        $newest = new home();
        $data['newest'] = $newest->get_allNews(0, 5);
        $data['flag'] = 0;
        $segS = $url->segment(2, false);
        if (!empty($segS)) {
            $segC = explode('-', $segS);
            $segQ = explode('.', $segC[count($segC) - 1]);
            $seg = $segQ[0];
        } else {
            $seg = $data['newest'][0]['id'];
            $data['flag'] = 1;
        }
        $new_info = new home();
        $data['new_info'] = $new_info->get_news($seg);
        $data['url'] = URL . 'tin-tuc/';
        $this->head(6);
        $this->banner();
        Controller::get_lib('template');
        Controller::get_view('tin_tuc', $data);

        $this->footer();
    }

    function temp($url, $segS = '') {
        $categories = new home();
        $result_cat = array();
        foreach ($categories->get_categories() as $value) {
            $result_cat['cate'][] = array($value['cat_name'], $value['file_img'], $value['id']);
        }
        $result_cat['url'] = URL . 'mau-website/';

        if ($segS === '') {
            $seg = $url->segment(2, false);
            if (!empty($seg)) {
                $aseg = explode('.', $seg);
                $result_cat['active'] = $aseg[1];
            } else {
                $result_cat['active'] = '';
            }
        } else {
            $result_cat['active'] = $segS;
        }
        Controller::get_lib('template');
        $result_cat['ran'] = 3;
        Controller::get_view('mau_website', $result_cat);
    }

    function ten_mien() {
        $this->head(5);
        $this->banner();
        $result['url'] = URL . 'mau-website/';
        $this->footer();
    }

    function lien_he() {
        $this->head(7);
        $this->banner();
        Controller::get_view('lien_he', $this->result);
        $this->footer();
    }

    function loi() {
        header('location:' . URL);
    }

}